import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, categoryType, status, page = 1, pageSize = 10 } = query;

  // 模拟分类数据
  const categories = [
    {
      id: '1',
      name: '客房资源',
      code: 'ROOM_CATEGORY',
      categoryType: 'BUSINESS',
      description: '酒店客房相关资源分类',
      resourceCount: 25,
      sort: 1,
      status: 'ACTIVE',
      createTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      name: '温泉资源',
      code: 'SPA_CATEGORY',
      categoryType: 'BUSINESS',
      description: '温泉洗浴相关资源分类',
      resourceCount: 12,
      sort: 2,
      status: 'ACTIVE',
      createTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      name: '餐饮资源',
      code: 'DINING_CATEGORY',
      categoryType: 'BUSINESS',
      description: '餐饮服务相关资源分类',
      resourceCount: 8,
      sort: 3,
      status: 'ACTIVE',
      createTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      name: '表单模板',
      code: 'FORM_TEMPLATE',
      categoryType: 'TECHNICAL',
      description: '表单模板技术资源分类',
      resourceCount: 15,
      sort: 4,
      status: 'ACTIVE',
      createTime: '2024-01-14 16:20:00',
    },
    {
      id: '5',
      name: '报表模板',
      code: 'REPORT_TEMPLATE',
      categoryType: 'TECHNICAL',
      description: '报表模板技术资源分类',
      resourceCount: 10,
      sort: 5,
      status: 'ACTIVE',
      createTime: '2024-01-14 15:10:00',
    },
  ];

  // 过滤数据
  let filteredData = categories;

  if (keyword) {
    filteredData = filteredData.filter(item =>
      item.name.includes(keyword as string) ||
      item.code.includes(keyword as string)
    );
  }

  if (categoryType) {
    filteredData = filteredData.filter(item =>
      item.categoryType === categoryType
    );
  }

  if (status) {
    filteredData = filteredData.filter(item =>
      item.status === status
    );
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
